package com.greenant.store.adapter;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.activity.OrderDetailActivity;
import com.greenant.store.bean.Order;
import com.greenant.store.bean.OrderItem;
import com.greenant.store.bean.StoreInfo;
import com.greenant.store.utils.Const;
import com.greenant.store.utils.UtilTools;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyOrderListAdapter extends BaseAdapter {

	private List<Order> list;
	private Context context;
	//BitmapUtils bitmapUtils;
	private String[] orderStatus;
	private final static String TAG = "MyOrderListAdapter";

	String cash_on_delivery = "";
	 DecimalFormat df = new DecimalFormat("#########.##");
	 
	 SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public MyOrderListAdapter(List<Order> list, Context ctx) {
		this.list = list;
		this.context = ctx;
		orderStatus = ctx.getResources().getStringArray(
				R.array.order_status_code);
		cash_on_delivery = context
				.getString(R.string.order_ensure_pay_method_cash_on_delivery);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.item_orderlist, null);
			viewHolder = new ViewHolder();

			viewHolder.orderNo = (TextView) convertView.findViewById(R.id.orderNo);
			viewHolder.userName = (TextView) convertView.findViewById(R.id.userName);
			viewHolder.time = (TextView) convertView.findViewById(R.id.time);
			viewHolder.status = (TextView) convertView.findViewById(R.id.status);

			viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final Order order = list.get(position);
		

		Date date = order.getCreatedAt();
		if(!TextUtils.isEmpty(order.getConsignee()))
		{
			viewHolder.userName.setText(context.getString(R.string.earning_recv_name)+order.getConsignee());
		}
		
		viewHolder.orderNo.setText(context.getString(R.string.orderdetail_number)+order.getOrderId());
		if(order.isHaveWeight())
		{
			if(order.isWeighted())
			{
				viewHolder.time.setText(format.format(order.getUpdatedAt())+" "+context.getString(R.string.orderlist_item_weighted));		
			}
			else{
				viewHolder.time.setText(format.format(order.getUpdatedAt())+" "+context.getString(R.string.orderlist_item_unweighted));
			}
		}
		else{
			viewHolder.time.setText(format.format(order.getUpdatedAt()));	
		}
		
		viewHolder.status.setText(UtilTools.getOrderStatusString(context, order));
		
		List<OrderItem> orderItems = order.getOrderItems();
		if (orderItems.size() > 0) {
			String name = "";
			String imageUrl="";
			for (int i = 0; i < orderItems.size(); i++) {
				String temp = orderItems.get(i).getName();
				if (!TextUtils.isEmpty(temp)) {
					name += (temp + "+");
				}
				String url=orderItems.get(i).getPicUrl();
				if(TextUtils.isEmpty(imageUrl))
				{
					imageUrl=url;
				}

			}
			if(TextUtils.isEmpty(imageUrl))
			{
				viewHolder.icon.setImageResource(R.drawable.pictures_no);
			}
			else{
				//bitmapUtils.display(viewHolder.icon, imageUrl);
				ImageLoader.getInstance().displayImage(imageUrl, viewHolder.icon,AntHomeApplication.imageDisplayOptions);
			}
			
		}else{
			viewHolder.icon.setImageResource(R.drawable.pictures_no);
		}

		// 跳转到订单详情页面
        convertView.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(context, OrderDetailActivity.class);
                Const.order=order;
                context.startActivity(intent);
            }
        });

		return convertView;
	}

	class ViewHolder {
		public TextView orderNo;
		public TextView userName;
		public TextView time;
		public TextView status;
		
		public ImageView icon;
		
	}

}
